#include "sensor.h"
#include <QPainter>

Sensor::Sensor(QGraphicsItem *parent)
    : CircuitComponent(parent)
{
    setFlag(QGraphicsItem::ItemIsSelectable);
}

QPointF Sensor::inputPort() const
{
    return QPointF(-30, 0);
}

QPointF Sensor::outputPort() const
{
    return QPointF(30, 0);
}

void Sensor::setText(const QString &text)
{
    m_text = text;
}

QRectF Sensor::boundingRect() const
{
    return QRectF(-35, -25, 70, 50);
}

void Sensor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

    // 绘制主体
    painter->setPen(QPen(Qt::darkGreen, 2));
    painter->setBrush(QColor(200, 255, 200));
    painter->drawEllipse(-20, -20, 40, 40);    

    // 绘制感应符号
    painter->setPen(QPen(Qt::red, 2));
    painter->drawLine(0, -15, 0, 15);  // 垂直线
    painter->drawLine(-15, 0, 15, 0);  // 水平线

    // 绘制箭头
    QPainterPath arrow;
    arrow.moveTo(0, 20);
    arrow.lineTo(-5, 15);
    arrow.lineTo(5, 15);
    arrow.closeSubpath();
    painter->fillPath(arrow, Qt::red);

    // 绘制端口
    painter->setPen(Qt::black);
    painter->drawRect(-30, -5, 10, 10); // 输入
    painter->drawRect(20, -5, 10, 10);  // 输出

    painter->setPen(QPen(Qt::white));
    //绘制文字
    painter->drawText(QRect(-25, -40, 120, 20), Qt::AlignLeft,m_text);
}
